<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Env;
use app\admin\model\UserTable as UserTableModel;
use app\admin\model\Module as ModuleModel;
use app\admin\model\Address as AddressModel;
use app\admin\model\Introduction as IntroductionModel;
use app\admin\model\Feedback as FeedbackModel;
use app\admin\model\FeedbackCate as FeedbackCateModel;
use app\admin\model\AboutUs as AboutUsModel;
use app\admin\model\Collection as CollectionModel;
use app\admin\model\UserPintuanLog;
class Mine extends Api
{
    protected $noNeedLogin = ['Mine','upload','addressFrom','delete','delCollect','feedBackFrom','helpCenterDetails'];
    protected $noNeedRight = ['*'];

    //我的
    public function Mine()
    {
        $resultArr = [
            'center' => $this->center(), //个人中心
            'modele' => $this->modele(),//金刚区模块名
            'address' => $this->address(),//收货地址显示
            'helpCenter' => $this->helpCenter(),//帮助中心列表
            'feedBack' => $this->feedBack(),//用户反馈显示
            'feedBackCate' => $this->feedBackCate(),//反馈类型
            'aboutUs' => $this->aboutUs(),//设置-关于我们、隐私协议
        ];
        return sucessMsg(200, '请求成功', $resultArr);
    }

    //上传头像
    public function upload()
    {
        $file = request()->file('image');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'avatar');
            if ($info) {
                $a = '/avatar/' . str_replace("\\", "/", $info->getSaveName());
                return sucessMsg('200','上传成功', $a);
            } else {
                echo $file->getError();
                return errMsg('400','上传失败');
            }
        }
        return errMsg('','请传入文件');
    }


    //个人中心
    public function center()
    {
        $model = new UserTableModel();
        $list = $model
            ->field('id,avatar,nickname,mobile,level_id,invite_code')
            ->find($this->auth->id);
        $list = addWebSiteUrl($list, ['avatar']);
        return $list;
    }

    //金刚区模块名
    public function modele()
    {
        $model = new ModuleModel();
        //我的页面除我的订单外
        $list = $model
            ->where('type', 2)
            ->field('id,name,image')
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        //我的页面我的订单
        $list1 = $model
            ->where('type', 3)
            ->field('id,name,image')
            ->select();
        $list1 = addWebSiteUrl($list1, ['image']);
        return
            [
                'list'=>$list,
                'list1'=>$list1
            ];
    }

    //地址簿，地址管理显示（地址管理表）
    public function address()
    {
        $model = new AddressModel();
        $list = $model
            ->where('status_switch', 1)
            ->field('id,nickname,mobile,city,site')
            ->order('id', 'desc')
            ->select();
        return $list;
    }

    //填写收货地址
    public function addressFrom()
    {
        $list = $this->request->param();
        if ($this->request->ispost() && !empty($list)) {
            $this->model = new AddressModel();

            if (!preg_match('^1(3|4|5|7|8)[0-9]\d{8}$^', $list['mobile'])) {
                return errMsg('手机号格式不正确');
            }
            $list['createtime'] = time();
            $list['updatetime'] = time();
            $about = $this->model->insert($list);
            if ($about) {
                return sucessMsg(200, '添加成功');
            }
            return errMsg(400, '添加失败');
        }
        return errMsg('请添加物流信息');
    }

    //(地址管理、地址簿 )删除地址信息
    public function delete()
    {
        $model = new AddressModel();
        $id = input('id/d', 0);
        $data = $model->where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        if ($data->save(['id' => $id, 'status_switch' => -1]))
            return sucessMsg(200, '删除成功');
        return errMsg(422, '删除失败');
    }

    //(地址管理、地址簿 )修改地址信息
//    public function update()
//    {
//        $date = input('post.');
//        try {
//            validate(AddressValidate::class)->scene('update')->batch()->check($date);
//            $model = new AddressModel();
//            if ($model->where(['id' => $date['id']])->save($date)) {
//                return sucessMsg(200, '提交成功');
//            }
//            return errMsg(400, '提交失败');
//        } catch (ValidateException $e) {
//            return errMsg(400, $e->getError());
//        }
//    }

    //商品收藏显示
    public function index()
    {
        $collect = CollectionModel::with('commodity')
            ->where('user_id', $this->auth->id)
            ->select();
        foreach ($collect as &$v) {
            $image = explode(',', $v['commodity']['image']);
            if (isset($image[0])) {
                $v['commodity']['image'] = Env::get('database.domin') . $image[0];
            }

        }
        $this->success('获取成功', $collect);
    }

    //商品收藏或取消收藏
    public function collect()
    {
        $goods = input('post.');
        if (empty($goods['commodity_id'])) {
            $this->error('参数错误');
        }
        $model = new CollectionModel();
        $a = CollectionModel::where(['user_id' => $this->auth->id, 'commodity_id' => $goods['commodity_id']])->find();
        if (!empty($a)) {
            $data = CollectionModel::destroy($a['id']);
            $this->success('取消收藏');
        } else {
            $model->user_id = $this->auth->id;
            $model->commodity_id = $goods['commodity_id'];
            $data = $model->save();
            $this->success('收藏成功');
        }

    }


    //商品收藏删除
    public function delCollect()
    {
        $ids = input('ids');
        if (empty($ids)) {
            $this->error('请选中需要删除的商品');
        }
        if (CollectionModel::where('id', 'in', $ids)->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

    //帮助中心
    public function helpCenter()
    {
        $model = new IntroductionModel();
        $list = $model
            ->where('type', 3)
            ->field('id,title,createtime,image')
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        return $list;
    }

    //我的帮助中心详情页
    public function helpCenterDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入id!');
        }
        $model = new IntroductionModel();
        $list = $model
            ->where('id', $id)
            ->field('id,title,content,image')
            ->find();
        if (empty($list)) {
            return errMsg(422, '找不到数据');
        }
        $list->image = Env::get('database.domin') . $list->image;
        return sucessMsg(200, '详情获取成功！', ['list' => $list]);
    }

    //反馈类型
    public function feedBackCate()
    {
        $model = new FeedBackCateModel();
        $list = $model
            ->field('id,name')
            ->select();
        return $list;
    }

    //用户反馈
    public function feedBackFrom()
    {
        $list = $this->request->param();
        if ($this->request->ispost() && !empty($list)) {
            $this->model = new feedBackModel();

            if (!preg_match('^1(3|4|5|7|8)[0-9]\d{8}$^', $list['phone'])) {
                return errMsg('手机号格式不正确');
            }
            $list['createtime'] = time();
            $list['updatetime'] = time();
            $about = $this->model->insert($list);
            if ($about) {
                return sucessMsg(200, ' 添加成功');
            }
            return errMsg(400, '添加失败');
        }
        return errMsg('请添加反馈信息');
    }

    //用户反馈显示
    public function feedBack()
    {
        $model = new FeedBackModel();
        $list = $model
            ->field('id,genre,content,image,createtime')
            ->select();
        return $list;
    }

    //设置-关于我们、隐私协议
    public function aboutUs()
    {
        $model = new AboutUsModel();
        //关于我们
        $list = $model
            ->where('type', 1)
            ->field('id,content')
            ->select();
        //隐私协议
        $list1 = $model
            ->where('type', 3)
            ->field('id,content')
            ->select();
        return [
            'list' => $list,
            'list1' => $list1
        ];
    }

    //拼购次数
    public function shipments()
    {
        $model = new UserPintuanLog();
        $list = $model
            ->where('status', 0)
            ->field('id')
            ->select();
        $list['total'] = count($list);
        return $list;
    }

    //拼中次数
    public function receiving()
    {
        $model = new UserPintuanLog();
        $list = $model
            ->where('status', 1)
            ->field('id')
            ->select();
        $list['total'] = count($list);
        return $list;
    }


}
